# $Id$
# Maintainer: Naoki Kawakami <nkawakami atto parallels dotto com>
# Contributor: Alex Talker
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Jakub Schmidtke <sjakub@gmail.com>

pkgname=firefox-hg
pkgver=314402+.c9971be9e981+
pkgrel=1
pkgdesc="Latest firefox browser built from the trunk code"
arch=('i686' 'x86_64')
license=('MPL' 'GPL' 'LGPL')
url="https://www.mozilla.org/firefox/"
conflicts=('firefox')
depends=('gtk2'
##'mozilla-common' #no because this sets this: export MOZ_PLUGIN_PATH="/usr/lib/mozilla/plugins"
'libxt' 'startup-notification' 'mime-types'
         'dbus-glib' 'alsa-lib' 'desktop-file-utils' 'hicolor-icon-theme'
         'libvpx' 'icu' 'libevent' 
         #'nss>=3.26'
         'hunspell' 'sqlite')
makedepends=('unzip' 'zip' 'diffutils' 'python2' 'yasm' 'mesa' 'imake'
             #'python2-dbus' # 6:07.59 Notification center failed: Install the python dbus module to get a notification when the build finishes.  # ok this does nothing! except not show me this message!
             #disabled pgo: 'xorg-server-xvfb'
             'libpulse' 'gst-plugins-base-libs'
#src: https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Linux_Prerequisites
'gtk2' 'libxt' 'dbus-glib' 'gcc>=4.8'
             'freetype2>2.1.0' 'pkg-config>=0.9.0' 'libnotify' 'libidl2'
#old             'iw' 'alsa-lib' 'curl' these are probably not needed?

             'ccache-git'
             #'ccache>=3.2.0' #this fails when ccache-git 3.3.1_2_g31c67cb-1 #it must support -fprofile-generate or else cache misses! 3.1.9 the git version works, but not the 3.1.9 non-git which is current now.
             'inetutils' 'mercurial' 'autoconf2.13' 'nss>=3.19.1' 'nspr>=4.10.8' 'libpng>=1.6.17')
optdepends=('networkmanager: Location detection via available WiFi networks'
            'gst-plugins-good: h.264 video'
            'gst-libav: h.264 video'
             'upower: Battery API')
#install=firefox.install
#patch1='911546_good.patch'
patch2='debug_exclude_3_files.patch'
patch3='byebyeplugins_autoload.patch'
patch4='dontcoredump_on_this.patch'
patch5='allow_js_to_lockPref_from_bug440908.patch'
#patch6='dumb.patch'
#patch7='bug1298459.patch'
#options=('!emptydirs' '!makeflags')
options=('!strip' '!debug' '!ccache' '!distcc') #XXX: yep, man PKGBUILD confirms ccache distcc should be in options array!!!
#BUILDENV=('!ccache') #oh don't use uppercase! it overrides /etc/makepkg.conf one! use lowercase it will merge!
#BUILDENV=('!ccache' 'distcc')
#buildenv=('!ccache' 'distcc') #not sure if this actually works; didn't work XXX: DON"T use these! use options array!
_hgfolder="$pkgname"
#source=('hg+http://hg.mozilla.org/mozilla-central'
source=("${_hgfolder}::hg+https://hg.mozilla.org/mozilla-central"
        'mozconfig'
#        'firefox.desktop'
        'vendor.js'
        'firefox-install-dir.patch'
#        "$patch1"
        "$patch2"
        "$patch3"
        "$patch4"
        "$patch5"
#        "$patch6"
#        "$patch7"
        )
sha256sums=('SKIP'
            '9a14af45a55c8c367b31ce35144a195e5edde6a69fc17e59ca84f3a52aa6f355'
            '57c3d8241222a0c91d38b0af702c63cbceab776868de85e4c3f9610544871e3a'
            '0c7d91472e9244e1e414bd9da7252e824e6d9f8872ad7a636ee85585ad6e3256'
            '32b5bf22a2869103222a7de9bb5fcc5c8cc253786c3f7e9e9ff5867f866bf0bf'
            '277f4203880f13a75970a22e410e39a23c868e14f895e6af74443fe9ba356cac'
            '462b2107a004df7357b62c9d305610ed6a314a556b8c3b1df1743aa9f36e2e79'
            '209e3b37484ccd338768c14e3a43ca78131802430d26a8c2f2686cac5fbf81e3')
#_pgo=1 # set to 0 to disable

pkgver() {
  cd "${_hgfolder}"
  echo $(hg identify -n).$(hg identify -i)
}

prepare() {
  cd "${_hgfolder}"

  #not .mozconfig anymore, src: https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Configuring_Build_Options
  cp -L -- "${srcdir}/mozconfig" mozconfig

  patch -Np1 -i "${srcdir}/firefox-install-dir.patch"
#  patch -Np1 -i "${srcdir}/${patch1}"
  patch -Np1 -i "${srcdir}/${patch2}"
  patch -Np1 -i "${srcdir}/${patch3}"
  patch -Np1 -i "${srcdir}/${patch4}"
  patch -Np1 -i "${srcdir}/${patch5}"
#  patch -Np1 -i "${srcdir}/${patch6}"
#  patch -Np1 -i "${srcdir}/${patch7}"

  # https://bugs.archlinux.org/task/41689
#  patch -Np2 -i ../rhbz-966424.patch

#  [[ -d "$srcdir/path" ]] || mkdir "$srcdir/path"

  # WebRTC build tries to execute "python" and expects Python 2
#  [[ -L "$srcdir/path/python" ]] || ln -s /usr/bin/python2 "$srcdir/path/python"

  # configure script misdetects the preprocessor without an optimization level
  # https://bugs.archlinux.org/task/34644
#  sed -i '/ac_cpp=/s/$CPPFLAGS/& -O2/' configure

  #probably not the fix, but rather AOM=>DOM in here: ./dom/webidl/AccessibleNode.webidl
#  if test -d 'accessible/aom'; then
#     mv -- 'accessible/aom' 'accessible/dom'
#  fi
}

build() {
  set|grep -i distcc
  cd "${_hgfolder}"

#  export PATH="$srcdir/path:$PATH" #not in prepare() so...
  export LDFLAGS="$LDFLAGS -Wl,-rpath,/usr/lib/firefox"
  export PYTHON="/usr/bin/python2"
  export MOZ_MAKE_FLAGS="$MAKEFLAGS"
  unset CPPFLAGS

# PGO will result in double compilation - disabled for now(due to a note in mozconfig that it'd lockup the build!)
# http://en.wikipedia.org/wiki/Profile-guided_optimization
# https://developer.mozilla.org/en/docs/Building_with_Profile-Guided_Optimization
  # Do PGO
#  if [[ $CARCH = x86_64 ]] && [[ $_pgo = 1 ]]; then
#    msg2 'PGO build is selected'
#    xvfb-run -a -s "-extension GLX -screen 0 1280x1024x24" \
#    make -f client.mk profiledbuild
#  else
#    msg2 'Non-PGO build is selected or your architecture is not x86_64'
#    make -f client.mk build
#  fi

  if ! ./mach -v build -v; then
    kill $!
    return 1
  fi

  kill $! || true
}

package() {
  cd "${_hgfolder}"

#oldway: (this restarts the whole build process, wtf?) make -f client.mk DESTDIR="$pkgdir" INSTALL_SDK= install
#newway:#doesn't work(destdir!)!  ./mach -v DESTDIR="$pkgdir" INSTALL_SDK= install
  export DESTDIR="$pkgdir"
  export INSTALL_SDK=
  ./mach -v install

  #TODO: add my default values for a bunch of about:config settings in this:
  install -Dm644 ../vendor.js "$pkgdir/usr/lib/firefox/browser/defaults/preferences/vendor.js"

#  for i in 16 32 48; do
#      install -Dm644 browser/branding/unofficial/default$i.png \
#        "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/firefox.png"
#  done
#  install -Dm644 browser/branding/unofficial/content/icon64.png \
#    "$pkgdir/usr/share/icons/hicolor/64x64/apps/firefox.png"
#  install -Dm644 browser/branding/unofficial/mozicon128.png \
#    "$pkgdir/usr/share/icons/hicolor/128x128/apps/firefox.png"
#  install -Dm644 browser/branding/unofficial/content/about-logo.png \
#    "$pkgdir/usr/share/icons/hicolor/192x192/apps/firefox.png"
#  install -Dm644 browser/branding/unofficial/content/about-logo@2x.png \
#    "$pkgdir/usr/share/icons/hicolor/384x384/apps/firefox.png"

#  install -Dm644 ../firefox.desktop \
#    "$pkgdir/usr/share/applications/firefox.desktop"

  # Use system-provided dictionaries
  rm -rf "$pkgdir"/usr/lib/firefox/{dictionaries,hyphenation}
  ln -s /usr/share/hunspell "$pkgdir/usr/lib/firefox/dictionaries"
  ln -s /usr/share/hyphen "$pkgdir/usr/lib/firefox/hyphenation"

  #workaround for now
  #https://bugzilla.mozilla.org/show_bug.cgi?id=658850
  ln -sf firefox "$pkgdir/usr/lib/firefox/firefox-bin"
}
